﻿#region Usings

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace DataInterpolator.Model
{
   public class ExportInterpInfo
   {
      private const double __NullValue = -999.25;

      public string Filename { get; set; }

      public string Company { get; set; }

      public string WellTitle { get; set; }

      public string Field { get; set; }

      public string Location { get; set; }

      public string API { get; set; }

      public string Country { get; set; }

      public string County { get; set; }

      public string State { get; set; }

      public string ServiceCompany { get; set; }

      public DateTime LogDate { get; set; }

      public string GenerateLAS(int index, DataSet data)
      {
         double startDepth = data.XOffset + data.TrueOffset;
         double stopDepth = data.XOffset + data.Values.Count * data.XSpacing + data.TrueOffset;
         double spacing = data.XSpacing;

         StringBuilder sb = new StringBuilder();

         sb.AppendLine("~Version");
         sb.AppendLine("VERS .        2      : CWLS LAS Version 2.0");
         sb.AppendLine("WRAP .        NO     : One Line per Depth Step  ");
         sb.AppendLine("~Well");
         sb.AppendLine("#MNEM.UNIT      DATA TYPE    INFORMATION");
         sb.AppendLine("#----------   ------------   ------------------------------");
         sb.AppendLine(" STRT.F                   " + FormatValue(startDepth, 12, 4) + "  : START Depth");
         sb.AppendLine(" STOP.F                   " + FormatValue(stopDepth, 12, 4) + "  : STOP DEPTH");
         sb.AppendLine(" STEP.F                   " + FormatValue(spacing, 12, 5) + "  : STEP LENGTH");
         sb.AppendLine(" NULL.                    " + FormatValue(__NullValue, 12, 4) + "  : NULL VALUE");
         sb.AppendLine(" COMP.     " + FormatField(Company, 27) + "  : COMPANY");
         sb.AppendLine(" WELL.     " + FormatField(WellTitle, 27, " #" + (index + 1).ToString()) + "  : WELL");
         sb.AppendLine(" FLD .     " + FormatField(Field, 27) + "  : FIELD");
         sb.AppendLine(" LOC .     " + FormatField(Location, 27) + "  : LOCATION");
         sb.AppendLine(" API .     " + FormatField(API, 27, (index + 1).ToString()) + "  : API REFERENCE NUMBER");
         sb.AppendLine(" CTRY.     " + FormatField(Country, 27) + "  : COUNTRY             ");
         sb.AppendLine(" CNTY.     " + FormatField(County, 27) + "  : COUNTY              ");
         sb.AppendLine(" STAT.     " + FormatField(State, 27) + "  : STATE               ");
         sb.AppendLine(" SRVC.     " + FormatField(ServiceCompany, 27) + "  : SERVICE COMPANY  ");
         sb.AppendLine(" DATE.     " + FormatField(LogDate.ToString("MM-dd-yyyy"), 27) + "  : LOG DATE          ");
         sb.AppendLine("~Curve");
         sb.AppendLine("#MNEM.UNIT       API CODE    CURVE DESCRIPTION");
         sb.AppendLine("#----------   ------------   ------------------------------");
         sb.AppendLine(" DEPT .F                                :DEPTH");
         sb.AppendLine(" DT   .US/F   60 520 32 00              :2 SONIC TRANSIT TIME");
         sb.AppendLine("~Other");
         sb.AppendLine("# ");
         sb.AppendLine("# ");

         //Now, determine the max num characters for each row
         List<int> colSizes = new List<int>(2);
         List<string> depthStr = new List<string>();
         List<string> dtStr = new List<string>();

         //Start the min size at 5 so that the headers arent squished
         colSizes.Add(5);
         colSizes.Add(5);

         for (int i = 0; i < data.Values.Count; i++)
         {
            double depthVal = data.XOffset + i * data.XSpacing + data.TrueOffset;

            string depth = FormatValue(depthVal, -1);
            string dt2 = FormatValue(data.Values[i], -1);

            colSizes[0] = Math.Max(colSizes[0], depth.Length);
            colSizes[1] = Math.Max(colSizes[1], dt2.Length);

            depthStr.Add(depth);
            dtStr.Add(dt2);
         }

         //For all cols other than 0, add 4 spaces between columns
         for (int i = 1; i < colSizes.Count; i++)
         {
            colSizes[i] += 4;
         }

         sb.AppendLine("#" + "DEPT".PadLeft(colSizes[0]) + "DT".PadLeft(colSizes[1]));
         sb.AppendLine("~A");

         for (int i = 0; i < data.Values.Count; i++)
         {
            sb.AppendLine(" " + depthStr[i].PadLeft(colSizes[0]) + dtStr[i].PadLeft(colSizes[1]));
         }

         return sb.ToString();
      }

      private string FormatField(string prefix, int max, string suffix = "")
      {
         //Shorten the prefix if necessary
         prefix = prefix.Substring(0, Math.Min(max - suffix.Length, prefix.Length));

         return (prefix + suffix).PadRight(max);
      }

      private string FormatValue(double value, int maxChars, int numDec = 4)
      {
         if (double.IsNaN(value))
         {
            value = __NullValue;
         }

         string formatter = "F" + numDec.ToString();

         string output = value.ToString(formatter);

         if (maxChars > 0)
         {
            output = output.PadLeft(maxChars);
         }

         return output;
      }
   }
}
